TagNavPageManager=function(){
	this.inited=false;
	this.options={};
	this.gridCardDataMap={};
	this.gridCardIdIndex=0;
}

TagNavPageManager.defValue={
	contentEl:"#tag-nav-grid-content"
}

TagNavPageManager.prototype.init=function(opts){
	var that=this;
	if(!this.inited){
		this.options=$.extend(true,{},TagNavPageManager.defValue,opts);
		mui("#tag-info-list-scroll-wrapper").scroll();
		this.loadGushiwenTags();
		this.initElesEvent();
	}
	this.inited=true;
}

TagNavPageManager.prototype.initElesEvent=function(){
	//
	$(this.options.contentEl).on('tap', '.tag-item-content .tag-item', function(e) {
		var tagId = $(this).attr('tag-id');
		var tagName = $(this).attr('tag-name');
		var tagImg = $(this).find('img').attr('src');
		mui.openWindowWithTitle({
			url: '../pages/tagGushiwenList.html?tagId=' + tagId,
			id: 'tagGushiwenList.html-tagId-' + tagId,
			extras: {
				tagId: tagId,
				tagName: tagName,
				tagImg: tagImg
			}
		});
	});
}

TagNavPageManager.prototype.loadGushiwenTags=function() {
	var that=this;
	var url = getShiciServiceHost() + '/shici/tagInfo/indexTagList';
	muiAjaxGetJsonData({
		url: url,
		data: {type:2},
		callback: function(data) {
			if (!data.entry || data.entry.length==0) {
				return;
			}
			//
			var gridCardDatList=[];
			mui.each(data.entry,function(index,itemValue){
				var gridCardId='gcid-'+that.gridCardIdIndex++;
				var gridCardData={
					gridCardId:gridCardId,
					gridCardName:itemValue.groupName,
					itemInfoList:itemValue.tagInfoList,
				}
				gridCardDatList.push(gridCardData);
				that.gridCardDataMap[gridCardId]=gridCardData;
			});
			//
			mui.each(gridCardDatList,function(index,itemValue){
				that.addGridCard(that.options.contentEl,itemValue);
			});
		}
	});
}
			
TagNavPageManager.prototype.addGridCard=function(content,gridData){
	if(!gridData.itemInfoList && gridData.itemInfoList.length==0){
		return;
	}
	var that=this;
	var tplStr=
	'<div class="mui-card">'
		+'<div class="xq-padding-content sub-content-title" grid-card-id="{gridCardId}">{gridCardName}</div>'
		+'<ul class="mui-table-view mui-grid-view mui-grid-9 tag-item-content" id="{gridCardId}">'
		+'</ul>'
	+'</div>';
	var gridCardName=gridData.gridCardName || '分类';
	var gridCardId=gridData.gridCardId;
	tplStr=tplStr.replace(new RegExp( '{gridCardName}' , "g" ),gridCardName);
	tplStr=tplStr.replace(new RegExp( '{gridCardId}' , "g" ),gridCardId);
	//
	$(content).append($(tplStr));
	var maxCount=4*3;//4列3行
	maxCount=maxCount<gridData.itemInfoList.length?maxCount:gridData.itemInfoList.length;
	for(var idx=0;idx<maxCount;idx++){
		var itemValue=gridData.itemInfoList[idx];
		that.addItemToGrid($("#"+gridCardId), itemValue);
	}
	/* mui.each(gridData.itemInfoList, function(index, itemValue) {
		addItemToGrid($("#"+gridCardId), itemValue);
	}); */
}

TagNavPageManager.prototype.addItemToGrid=function(gridContent, itemInfo) {
	var tplStr = '' +
		'<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3">' +
			'<a class="tag-item" tag-id={tagId} tag-name={tagName}>' +
				'<span class="xq-grid-item">' +
					'<img src="{imgUrl}" />' +
				'</span>' +
				'<div class="mui-media-body">{tagName}</div>' +
			'</a>' +
		'</li>';
	tplStr = tplStr.replace('{tagId}', itemInfo['id']);
	tplStr = tplStr.replace('{imgUrl}', itemInfo['imgUrl']);
	tplStr = tplStr.replace('{tagName}', itemInfo['tagName']);
	tplStr = tplStr.replace('{tagName}', itemInfo['tagName']);
	//
	$(gridContent).append($(tplStr));
}

var defGridItemInfoList = [{
		id: 121,
		tagName: '唐诗三百首',
		imgUrl: 'images/list-pic001.jpg'
	},
	{
		id: 158,
		tagName: '宋词精选',
		imgUrl: 'images/list-pic002.jpg'
	},
	{
		id: 494,
		tagName: '辞赋精选',
		imgUrl: 'images/list-pic004.jpg'
	},
	{
		id: 340,
		tagName: '楚辞',
		imgUrl: 'images/list-pic003.jpg'
	},
	{
		id: 472,
		tagName: '诗经',
		imgUrl: 'images/list-pic004.jpg'
	},
	{
		id: 4,
		tagName: '乐府',
		imgUrl: 'images/list-pic004.jpg'
	},
	{
		id: 180,
		tagName: '小学古诗',
		imgUrl: 'images/list-pic004.jpg'
	},
	{
		id: 181,
		tagName: '小学文言文',
		imgUrl: 'images/list-pic004.jpg'
	}
];